//
//  LFTabBarController.m
//  LoveFreshBee
//
//  Created by 王忠迪 on 10/15/16.
//  Copyright © 2016 王忠迪. All rights reserved.
//

#import "LFTabBarController.h"
#import "LFBaseTableViewController.h"
#import "LFHomeViewController.h"
#import "LFMarketViewController.h"
#import "LFShoppingCartViewController.h"
#import "LFMeViewController.h"
#import "LFBaseNavigationViewController.h"
#import "LFBaseTabBar.h"

@interface LFTabBarController ()

@end

@implementation LFTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    LFBaseTabBar *tabBar = [[LFBaseTabBar alloc] init];
    
    tabBar.block = ^{
        
        LFShoppingCartViewController *cartVC = [[LFShoppingCartViewController alloc] init];
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:cartVC];
        
        [self presentViewController:nav animated:YES completion:nil];
        
    };
    
    tabBar.backgroundColor = self.tabBar.backgroundColor;
    
    [self setValue:tabBar forKey:@"tabBar"];
    
    [self addChildViewControllers];
    
    //注册监听
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(gotoshopping) name:KgotoShopping object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addChildViewControllers {
    
    [self addChildViewController:[[LFHomeViewController alloc] init] image:@"v2_home" title:@"首页" index: 0];
    
    [self addChildViewController:[[LFMarketViewController alloc] init] image:@"v2_order" title:@"闪电超市" index: 1];
    
//    [self addChildViewController:[[LFShoppingCartViewController alloc] init] image:@"shopCart" title:@"购物车"];
//    
    [self addChildViewController:[[LFMeViewController alloc] init] image:@"v2_my" title:@"我的" index: 2];
    
}

- (void)addChildViewController:(UIViewController *)vc image:(NSString *)image title:(NSString *)title index:(NSInteger)index {
    
//    vc.navigationItem.title = title;
    
    vc.tabBarItem.title = title;
    
    UIColor *gray = [UIColor grayColor];
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:gray} forState:UIControlStateNormal];
    
    [vc.tabBarItem setImage:[UIImage imageNamed:image]];
    
    UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [vc.tabBarItem setSelectedImage:selectedImage];
    
    vc.tabBarItem.tag = index;
    
    LFBaseNavigationViewController *nav = [[LFBaseNavigationViewController alloc] initWithRootViewController:vc];
        
    [self addChildViewController:nav];
    
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    __block NSInteger index = 0 ;
    
    for (UIView * subView in tabBar.subviews) {
        
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (index == item.tag) {
                
                [subView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    if ([obj isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                        
                        obj.transform = CGAffineTransformMakeScale(0.4, 0.4);
                        
                        [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:UIViewAnimationOptionTransitionNone animations:^{
                            
                            obj.transform = CGAffineTransformMakeScale(1.0, 1.0);
                            
                        } completion:^(BOOL finished) {
                            
                        }];
                    }
                    
                    
                }];
                
            }
            
            index += 1;
        }
        
    }
    
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)gotoshopping{
    

    self.selectedIndex = 1;
    
}



@end
